#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : write_worker.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/4/24 13:56
"""

from PyQt5.QtCore import QThread, pyqtSignal

from utils.tools import request
from config import Config


class WriteWorkerQtThread(QThread):
    """写作"""
    failed = pyqtSignal(Exception)
    result  = pyqtSignal(str)


    def __init__(self, channel, src, desc, headers, parent=None):
        super(WriteWorkerQtThread, self).__init__()
        self.channel = channel
        self.src = src
        self.desc = desc
        self.headers = headers


    def run(self):
        try:
            url = f"{Config.Host}/api/write/general/"
            data = {
                "p": self.channel,
                "t": self.src,
                "desc": self.desc
            }
            response = request(url, method="POST", data=data, headers=self.headers)
            assert response.json().get("status") == 1
            self.result.emit(response.json().get("data").get("content"))
        except Exception as e:
            self.failed.emit(e)

